﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.IAlertsB;
using StartsEAS_API.DAL.AlertsD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.AlertsB
{
    public class AlertsBLL : IAlertsBLL
    {
        private readonly StarsEASContext _context;

        public AlertsDAL _alertsDal;

        public AlertsBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _alertsDal = new(_context);
        }

        public ResultUtils GetAllAlert()
        {
            return _alertsDal.GetAllAlert();
        }

        public ResultUtils GetAllAlertByStudent()
        {
            return _alertsDal.GetAllAlertByStudent();
        }

        public ResultUtils InsertAlerts(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            Alert alert = JsonConvert.DeserializeObject<Alert>(jsonStr);
            return _alertsDal.InsertAlerts(alert);
        }

        public ResultUtils UpdateAlerts(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            Alert alert = JsonConvert.DeserializeObject<Alert>(jsonStr);
            return _alertsDal.UpdateAlerts(alert);
        }

        public ResultUtils GetAllAlertByToken(string token) { 
            return _alertsDal.GetAllAlertByToken(token);
        }
    }
}
